<?php
namespace utils;

class PathTool
{
	public static function getName($path)
	{		
		$pos = strrpos($path,"\\");
		if($pos === false) $pos = strrpos($path,"/");
		if($pos === false) return $path;
		
		return substr($path,$pos+1);
	}
	
	public static function getExtention($path)
	{	
		if(!strpos($path,".")) return "";
		
		$ext="";
		$parts = pathinfo( $path);
		if(isset($parts["extension"]))
		$ext = $parts["extension"];//ext,jpg,gif,exe
		//$ext = strtolower($ext);
		return $ext;
	}
	
	public static function urlencode_safe($txt)
	{
		$txt = str_replace("\\", "/", $txt);//urlencode会将\\解析成\ 
		$txt = urlencode($txt);
		return str_replace("+","%20",$txt);
	}
	
	public static function url_safe_encode($txt)
	{
	    $txt = str_replace("\\", "/", $txt);//urlencode会将\\解析成\
	    $txt = urlencode($txt);
	    $txt = str_replace("+","%20",$txt);
	    $txt = str_replace("*","%2A",$txt);
	    $txt = str_replace("%7E","~",$txt);
	    $txt = str_replace("%2F","/",$txt);
	    return $txt;
	}
	
	public static function urlencode_path($txt)
	{
		$txt = str_replace("\\", "/", $txt);//urlencode会将\\解析成\
		$txt = str_replace("/", "\\\\", $txt); 
		$txt = urlencode($txt);
		$txt = str_replace("+","%20",$txt);
		return $txt;		
	}
	
	public static function urldecode_path($txt)
	{
		$txt = str_replace("+","%20",$txt);
		$txt = urldecode($txt);
		return $txt;				
	}
	
	public static function getBase64($name)
	{
	    if(!empty($name))
	    {
	        $s = trim($name);
	        $s = base64_decode($s);
	        return $s;
	    }	    
	    return $name;
	}
	
	public static function combin($p1,$p2)
	{
		$p1 = trim($p1);
		$p2 = trim($p2);

		if(PathTool::endsWith($p1,"/") || PathTool::endsWith($p1,"\\"))
			$p1 = substr($p1,0,strlen($p1)-1);
			
		if(PathTool::startsWith($p2,"\\") || PathTool::startsWith($p2,"/"))
			$p2 = substr($p2,1);
		
		return $p1 . "/" . $p2;
	}
	
	public static function startsWith($p1,$p2)
	{
		$p1 = trim($p1);
		$p2 = trim($p2);
		if(strlen($p1)==0 || strlen($p2)==0) return false;
		return substr($p1,0,1)==$p2;
	}
	
	public static function endsWith($a,$b)
	{
		$a = trim($a);
		$b = trim($b);
		if(strlen($a)==0||strlen($b)==0) return false;

	    return substr($a,-1,1)==$b;
	}
	
	public static function to_utf8($str)
	{
		$encode = mb_detect_encoding($str, array('ASCII','GB2312','GBK','UTF-8'));
		if( $encode == "UTF-8" ) return $str;
	
		return iconv($encode, "UTF-8", $str);
	}
	
	public static function to_gbk($str)
	{
		$encode = mb_detect_encoding($str, array('ASCII','GB2312','GBK','UTF-8'));
		if( $encode != "UTF-8" ) return $str;
	
		return iconv($encode, "GB2312", $str);
	}
	
	//将多字节转换为utf8
	public static function unicode_decode($str)
	{
		return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
				create_function(
						'$matches',
						'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
				),
				$str);
	}
	
	public static function BytesToString($byteCount)
	{
	    $suf = array( "B", "KB", "MB", "GB", "TB", "PB", "EB" );
	    if ($byteCount == 0)
	        return "0" + $suf[0];
        $bytes = abs($byteCount);
        $place = (int)(floor(log($bytes, 1024)));
        $num = round($bytes / pow(1024, $place), 1);
        return $num. $suf[$place];
	}

	public static function mkdirs($dir)
	{
	    if(is_dir($dir)) return true;
		if(!mkdir($dir,0777,true))
		{
			return false;
		}

		return true;
	}
	

	public static function mkdirsFromFile(/*string*/$f)
	{
		$dir = dirname($f);
		PathTool::mkdirs($dir);
	}
}
?>